import random
from datetime import datetime


class HotUserQuery:

    def __init__(self, gender=None, role=None, user_id=None):
        self.gender = gender
        self.user_id = user_id
        self.role = role

    def to_query(self):
        filter = []
        must_not = []
        if self.user_id:
            must_not.append({
                'term': {
                    '_id': self.user_id
                }
            })
        filter.append({
            'term': {
                'avatar_real': {
                    'value': True
                }
            }
        })
        if self.role and self.role != 'all':
            filter.append({
                'term': {
                    'role': {
                        'value': self.role
                    }
                }
            })
        if self.gender and self.gender != 'all':
            filter.append({
                'term': {
                    'gender': {
                        'value': self.gender
                    }
                }
            })
        return {
            'query': {
                'function_score': {
                    'query': {
                        'bool': {
                            'filter': filter,
                            'must_not': must_not
                        }
                    },
                    'score_mode': 'sum',
                    "boost_mode": 'sum',
                    'functions': [
                        {
                            "random_score": {
                                "seed": random.randint(0, 10000),
                                "field": "_id"
                            }
                        },
                        {
                            "exp": {
                                "last_active": {
                                    "origin": datetime.now().strftime("%Y-%m-%d"),
                                    "scale": "10d",
                                    "offset": "3d",
                                    "decay": 0.3
                                }
                            },
                            "weight": 1
                        }
                    ]
                }
            },
            '_source': ['_id']
        }


class NearbyUserQuery:
    def __init__(self, lat, lng, gender=None, role=None):
        self.lat = lat
        self.lng = lng
        self.gender = gender
        self.role = role

    def to_query(self):
        filter = []
        if self.gender and self.gender != 'all':
            filter.append({
                'term': {
                    'gender': {
                        'value': self.gender
                    }
                }
            })

        filter.append({
            'term': {
                'avatar_real': {
                    'value': True
                }
            }
        })
        if self.role and self.role != 'all':
            filter.append({
                'term': {
                    'role': {
                        'value': self.role
                    }
                }
            })
        return {
            "sort": [
                {
                    "_geo_distance": {
                        "location": {
                            "lat": self.lat,
                            "lon": self.lng
                        },
                        "order": "asc",
                        "unit": "km",
                        "mode": "min",
                        "distance_type": "arc"
                    }
                }
            ],
            "query": {
                "bool": {
                    "filter": filter
                }
            },
            '_source': ['_id']
        }


class AVCallUserQuery:

    def __init__(self, gender=None, user_id=None):
        self.gender = gender
        self.user_id = user_id

    def to_query(self):
        filter = []
        must_not = []
        if self.user_id:
            must_not.append({
                'term': {
                    '_id': self.user_id
                }
            })
        filter.append({
            'term': {
                'avatar_auth': {
                    'value': True
                }
            }
        })
        filter.append({
            'term': {
                'call_enabled': {
                    'value': True
                }
            }
        })
        filter.append({
            'term': {
                'gender': {
                    'value': 'g'
                }
            }
        })

        return {
            'query': {
                'function_score': {
                    'query': {
                        'bool': {
                            'filter': filter,
                            'must_not': must_not
                        }
                    },
                    'score_mode': 'sum',
                    "boost_mode": 'sum',
                    'functions': [
                        {
                            "random_score": {
                                "seed": random.randint(0, 10000),
                                "field": "_id"
                            }
                        },
                        {
                            "exp": {
                                "last_active": {
                                    "origin": datetime.now().strftime("%Y-%m-%d"),
                                    "scale": "10d",
                                    "offset": "3d",
                                    "decay": 0.3
                                }
                            },
                            "weight": 1
                        }
                    ]
                }
            },
            '_source': ['_id']
        }


class HotPostQuery:
    def __init__(self, gender=None, user_id=None):
        self.gender = gender
        self.user_id = user_id

    def to_query(self):
        filter = []
        must_not = []
        if self.user_id:
            must_not.append({
                'term': {
                    '_id': self.user_id
                }
            })
        if self.gender:
            filter.append({
                'term': {
                    'gender': {
                        'value': self.gender
                    }
                }
            })
        return {
            'query': {
                'function_score': {
                    'query': {
                        'bool': {
                            'filter': filter,
                            'must_not': must_not
                        }
                    },
                    'score_mode': 'sum',
                    "boost_mode": 'sum',
                    'functions': [
                        {
                            "random_score": {
                                "seed": random.randint(0, 10000),
                                "field": "_id"
                            }
                        }
                    ]
                }
            },
            '_source': ['_id']
        }
